package com.enjoyor.soa.traffic.frame.monitor;

import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
@PropertySource("classpath:application.properties")
@EnableScheduling
public class SystemStatusDubboMonitor {
	 @Value("${monitor.heartbeat:}")
	    private String monitor_heartbeat;
	
	 
	    
		 @Scheduled(cron = "0 0/1 * * * ?")
		 public void hearbeat() {
			 System.out.println("SystemStatusDubboMonitor");
			 if(monitor_heartbeat != null && !monitor_heartbeat.equals("")) {
				 try {
					String url = monitor_heartbeat;
					 String body = sendGetData(url, "utf-8");
				 }catch(Exception e) {
					 System.out.println("monitor_heartbeat error");
					 
				 }
			 }
		 }
		 
		 public String sendGetData(String url, String encoding) throws Exception { 
			 String result = ""; // 创建httpclient对象  
			 CloseableHttpClient httpClient = HttpClients.createDefault(); // 创建get方式请求对象
			 HttpGet httpGet = new HttpGet(url); httpGet.addHeader("Content-type", "application/json"); // 通过请求对象获取响应对象 
			 CloseableHttpResponse response = httpClient.execute(httpGet); // 获取结果实体
			 // 判断网络连接状态码是否正常(0--200都数正常) 
			 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				 result = EntityUtils.toString(response.getEntity(), "utf-8"); } // 释放链接 
			 response.close(); return result; 
		}
	
}
